Grokking Python Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Course Overview
Python Basics
Python Overview
Python Application Areas
Your First Python Program
Python - Basic Syntax
Python Comments
Quiz
Python Variables and Operators
Python - Variables
Python - Data types
Python - Numbers
Python - Booleans
Python - Type Conversions
Python - Operators
Quiz
Python Conditionals
Introduction to Conditional Statements in Python
Python - If...Else
Python - Match Case
Quiz
Python loops
Introduction to loops in Python
Python - for loop
Python - while loop
Python - Loop Control
Python - for else and while else loop
Quiz
Python Strings
Introduction to Strings in Python
Python - String Operators
Python Slicing Strings
Python - String Methods
Quiz
Python Array
Introduction to Array in Python
Python - Array Methods
Quiz
Python Lists
Introduction to Lists in Python
Python - Add and Remove List Items
Python - List Comprehension
Python - List Methods
Quiz
Introduction to Tuple in Python
Python - Unpack Tuple Items
Python - Join Tuples
Python - Tuple Methods
Quiz
Introduction to Dictionary in Python
Python - Nested Dictionaries
Python - Dictionary Methods
Quiz
Introduction to set in Python
Python - set Operations
Python - Set Methods
Quiz
Introductions to Function in Python
Python - Function Arguments
Recursive Functions
Python - Variable scope
Importing Modules
Quiz
Python - List Methods

Lists in Python are equipped with a variety of methods designed to facilitate the manipulation of their contents. These methods provide functionalities for adding items, removing items, sorting, and more, making lists highly versatile for managing collections of data in Python programs.

List Methods

Here’s a table summarizing common list methods along with rephrased descriptions:

MethodDescriptionTime Complexity
append(obj)Adds an item obj to the end of the list.
clear()Empties the list, removing all its items.
copy()Creates a duplicate of the list.
count(obj)Tallies the occurrence of obj within the list.
extend(seq)Merges another iterable seq into the list.
index(obj)Finds the first appearance of obj and returns its position.
insert(index, obj)Inserts obj at the specified index.
pop(obj=list[-1])Extracts and returns the item at obj index, defaults to the last item. for last item, for other positions
remove(obj)Deletes the first occurrence of obj from the list.
reverse()Inverts the order of items in the list.
sort([func])Organizes the list items. If func is provided, it uses this function for sorting.

Example 1: Using append()

The append() method is used to add an item to the end of a list. This is particularly useful when building up a list dynamically.

Python3

. . . .

Explanation:

  • my_pets = ['dog', 'cat']: Initializes the list with two elements: 'dog' and 'cat'.
  • my_pets.append('rabbit'): Adds 'rabbit' to the end of the my_pets list.
  • The print statement outputs the updated list with the newly appended item.

Example 2: Using remove()

The remove() method deletes the first occurrence of a specified item from the list. It is useful for removing unwanted elements.

Python3

. . . .

Explanation:

  • my_colors = ['red', 'blue', 'green', 'blue']: Starts with a list that includes 'blue' twice.
  • my_colors.remove('blue'): Removes the first 'blue' from the list.
  • The final print statement shows the list after the first 'blue' has been removed, demonstrating that only the first instance was deleted.

These examples highlight how list methods can be utilized to manage and manipulate list data effectively. By using these built-in methods, Python developers can perform a wide range of list operations efficiently and intuitively.

Python - List Comprehension
Quiz
Mark as Completed

On this page

List Methods

Example 1: Using append()

Example 2: Using remove()